package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import br.com.edumobi.R;
import br.com.edumobi.dados.modelos.Aluno;
import br.com.edumobi.utils.Mask;

public class ContatoAlunoFragment extends GenericoFragment {

    private TextView textViewNomeTelaContatoAluno;
    private EditText editTextTelefoneResidencialAluno;
    private EditText editTextTelefoneCelularAluno;
    private Button buttonSalvarContatoAluno;
    private ProgressBar progressBarContatoAluno;

    private ContatoAlunoTask task;
    private AcoesContatoAlunoFragment activity;
    private Aluno aluno;

    View.OnClickListener clickButtonSalvarContatoAluno = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            String telefoneResidencial = editTextTelefoneResidencialAluno.getText().toString();
            String telefoneCelular = editTextTelefoneCelularAluno.getText().toString();

            if(activity.isConnected()){
                aluno.telefoneResidencial = telefoneResidencial;
                aluno.telefoneCelular = telefoneCelular;

                task = new ContatoAlunoTask();
                task.execute();
            }else{
                activity.onSpeakAndMakeText(R.string.excecao_sem_conexao_internet);
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity = (AcoesContatoAlunoFragment) getActivity();

        aluno = activity.getAluno();

        editTextTelefoneResidencialAluno.setText(aluno.telefoneResidencial);
        editTextTelefoneCelularAluno.setText(aluno.telefoneCelular);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contato_aluno, container, false);

        textViewNomeTelaContatoAluno = (TextView) view.findViewById(R.id.textViewNomeTelaContatoAluno);
        editTextTelefoneResidencialAluno = (EditText) view.findViewById(R.id.editTextTelefoneResidencialAluno);
        editTextTelefoneCelularAluno = (EditText) view.findViewById(R.id.editTextTelefoneCelularAluno);
        buttonSalvarContatoAluno = (Button) view.findViewById(R.id.buttonSalvarContatoAluno);

        editTextTelefoneResidencialAluno.addTextChangedListener(Mask.insert("(##) #### ####", editTextTelefoneResidencialAluno));
        editTextTelefoneCelularAluno.addTextChangedListener(Mask.insert("(##) #### ####", editTextTelefoneCelularAluno));
        buttonSalvarContatoAluno.setOnClickListener(clickButtonSalvarContatoAluno);
        progressBarContatoAluno = (ProgressBar) view.findViewById(R.id.progressBarContatoAluno);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            hideComponents();
            progressBarContatoAluno.setVisibility(View.VISIBLE);
        }

        return view;
    }
    @Override
    public void showComponents() {
        textViewNomeTelaContatoAluno.setVisibility(View.VISIBLE);
        editTextTelefoneResidencialAluno.setVisibility(View.VISIBLE);
        editTextTelefoneCelularAluno.setVisibility(View.VISIBLE);
        buttonSalvarContatoAluno.setVisibility(View.VISIBLE);
        progressBarContatoAluno.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideComponents() {
        textViewNomeTelaContatoAluno.setVisibility(View.GONE);
        editTextTelefoneResidencialAluno.setVisibility(View.GONE);
        editTextTelefoneCelularAluno.setVisibility(View.GONE);
        buttonSalvarContatoAluno.setVisibility(View.GONE);
        progressBarContatoAluno.setVisibility(View.GONE);
    }

    private class ContatoAlunoTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            hideComponents();
            progressBarContatoAluno.setVisibility(View.VISIBLE);
            activity.onSpeak(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... voids) {
            return activity.onSalvarDadosAluno(aluno);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            showComponents();
            progressBarContatoAluno.setVisibility(View.GONE);

            if (aBoolean) {
                activity.onSpeakAndMakeText(R.string.dados_salvos_sucesso);
                activity.putPreferencesAluno(aluno);
            } else {
                activity.onSpeakAndMakeText(R.string.excecao_atualizacao_dados_aluno_malsucedida);
            }
        }
    }

    public interface AcoesContatoAlunoFragment extends AcoesGenericoFragment{
        Aluno getAluno();
        Boolean onSalvarDadosAluno(Aluno aluno);
    }
}